/**
 * Created with IntelliJ IDEA.
 * User: mkadush
 * Date: 12.09.13
 * Time: 8:29
 * To change this template use File | Settings | File Templates.
 */
var buildCtxMenu = function () {
    return new Ext.menu.Menu({
        items: [
            {
                itemId: 'add',
                handler: onAdd
            },
            {
                itemId: 'edit',
                handler: onEdit
            },
            {
                itemId: 'delete',
                handler: onDelete
            }
        ]
    });
};


var onCtxMenu = function (node, evtObj) {
    node.select();
    evtObj.stopEvent();

    if (!this.ctxMenu) {
        this.ctxMenu = buildCtxMenu();
    }
    var ctxMenu = this.ctxMenu;
    var addItem = ctxMenu.getComponent('add');
    var editItem = ctxMenu.getComponent('edit');
    var deleteItem = ctxMenu.getComponent('delete');

    var structureToAdd = '';
    var structureToEditDelete = '';

    if (node.id == 'org_') {
        editItem.disable();
        deleteItem.disable();
        addItem.enable();
        structureToAdd = 'отдел';
        structureToEditDelete ='организацию';
    } else if (node.leaf) {
        addItem.disable();
        editItem.enable();
        deleteItem.enable();
        structureToAdd = '';
        structureToEditDelete = 'человека';
    } else {
        if (node.id.substring(0, 4) == 'dep_') {
            structureToAdd = 'проект';
            structureToEditDelete = 'отдел';
        } else {
            structureToAdd = 'человека';
            structureToEditDelete = 'проект';
        }
        addItem.enable();
        editItem.enable();
        deleteItem.enable();
    }

    addItem.setText('добавить ' + structureToAdd);
    editItem.setText('переименовать ' + structureToEditDelete);
    deleteItem.setText('удалить ' + structureToEditDelete);

    ctxMenu.showAt(evtObj.getXY());
};